#include "z_list.h"
#include <stdlib.h>
struct list_head * new_list_head() {
	struct list_head * head = 
		(struct list_head *)malloc(sizeof(struct list_head));
	head->next = head->prev = head;
	return head;
}
void free_list_head(struct list_head * head) {
	free(head);
}
int list_empty(const struct list_head *head) {
	return head->next == head;
}
size_t list_length(const struct list_head *head) {
	size_t count = 0;
	struct list_head *itr = head->next;
	while (itr != head) {itr = itr->next; count++;}
	return count;
}
void __list_add(struct list_head *node, struct list_head *prev,
                struct list_head *next) {
	node->prev = prev;
	node->next = next;
	prev->next = node;
	next->prev = node;
}
void list_add(struct list_head *head, struct list_head *node) {
	__list_add(node,head,head->next);
}
void list_add_tail(struct list_head *head, struct list_head *node) {
	__list_add(node,head->prev,head);
}
struct list_head * __list_del(struct list_head *prev, struct list_head *next) {
	struct list_head *node = prev->next;
	prev->next = next;
	next->prev = prev;
	return node;
}
struct list_head * list_del(struct list_head *head) {
	return __list_del(head,head->next->next);
}
